package com.example.demo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.BussinessDao;
import com.example.demo.dao.WorkerDao;
import com.example.demo.pojo.Business;
import com.example.demo.pojo.Message;
import com.example.demo.pojo.Worker;
import com.example.demo.service.LoginService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    BussinessDao bussinessDao;
    @Resource
    WorkerDao workerDao;
    @Override
    public String Login(HttpSession session,String account, String pass) {

        Message message = new Message();
        account = account.replaceAll(" ","");
        pass = pass.replaceAll(" ","");

        session.setAttribute("account",account);


        int len = account.length();
        if (len == 11) {
            account = account.replaceAll("0","");
            String count = account.substring(0, 2);
            String seq = account.substring(2);
            session.setAttribute("count",count);

            if (count.equals("BU")) {
                Business business = bussinessDao.businessLogin(count, seq, pass);
                if (business != null) {
                    message.setAccount("BU");
                    message.setInf("登录成功");
                    session.setAttribute("business", business);
                }
            }
            if (count.equals("GR")) {

                Worker worker = workerDao.workerLogin(count, seq, pass);
                if (worker != null) {

                    message.setAccount("GR");
                    message.setInf("登录成功");
                    session.setAttribute("worker", worker);
                }
            }
            else {
                message.setInf("账户或密码错误");
            }
        }else {
            message.setInf("账户或密码错误");
        }
        String js = JSONObject.toJSONString(message);
        return js;
    }

    @Override
    public String checkLogin(HttpSession session) {
        String account = (String) session.getAttribute("account");

        Message message = new Message();


        if(account!=null){
            message.setAccount(account);
            message.setInf("已存在");
            System.out.println(1111);
            return JSONObject.toJSONString(message);
        }else{
            message.setInf("不存在");
            return JSONObject.toJSONString(message);
        }
    }

}

